package com.metalrain.alm.ui.fragments;

import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.metalrain.alm.R;
import com.metalrain.alm.service.LCA_Service;
import com.metalrain.alm.ui.LightOptionsActivity;
import com.metalrain.alm.ui.adapters.MyLightsAdapter;


public class LightGroupsFragment extends Fragment {
    private AdapterView listView;
    BroadcastReceiver serviceUpdated = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("LightGroupsFragment", "Service updated");
            listView.setAdapter(listView.getAdapter());
        }


    };

    public LightGroupsFragment() {}


    @Override
    public void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(serviceUpdated);
    }

    @Override
    public void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(serviceUpdated, new IntentFilter(LCA_Service.BROADCAST_HUE_SERVICE_UPDATED));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup v = (ViewGroup) inflater.inflate(R.layout.fragment_light_group, container, false);
        listView = (AdapterView) v.findViewById(R.id.listView);
        listView.setAdapter(new MyLightsAdapter(getActivity()));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                LightOptionsActivity.start(getActivity(), i);
            }
        });
        return  v;
    }
}
